package zmee.pmas.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.TextEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.pmgt.pmbs.common.enums.ConclusionEnum;
import kd.pmgt.pmbs.common.enums.StatusEnum;

import java.util.Iterator;

/**
* @author lijiawang
* @Date 2022/12/13 22:10
* @Description:考量指标
*/
public class PmscProjectevalFormPlugin extends AbstractFormPlugin {

    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String propertyName = e.getProperty().getName();
        byte var4 = -1;
        switch(propertyName.hashCode()) {
            case -309310695:
                if (propertyName.equals("project")) {
                    var4 = 0;
                }
                break;
            case 40622848:
                if (propertyName.equals("thirdevaluate")) {
                    var4 = 1;
                }
        }

        switch(var4) {
            case 0:
                this.projectChange();
                break;
            case 1:
                ChangeData[] changeSet = e.getChangeSet();
                Boolean newValue = (Boolean)changeSet[0].getNewValue();
                TextEdit auditUnit = (TextEdit)this.getControl("evaluateunit");
                auditUnit.setMustInput(newValue);
        }

    }

    private void projectChange() {
        this.getModel().deleteEntryData("indexentry");
        DynamicObject project = (DynamicObject)this.getModel().getValue("project");
        if (project != null) {
            QFilter reportProFilter = new QFilter("project", "=", project.getPkValue());
            QFilter resultFilter = (new QFilter("fsresult", "=", ConclusionEnum.PASS_S.getValue())).or("approvalresult", "=", ConclusionEnum.PASS_S.getValue());
            QFilter proFilter = new QFilter("pro", "=", project.getPkValue());
            QFilter statusFilter = new QFilter("billstatus", "=", StatusEnum.CHECKED.getValue());
            DynamicObject report = BusinessDataServiceHelper.loadSingle("pmfs_reportbill", "indexentry,indexentry.indexname,indexentry.declarevalue,indexentry.indexwarnlight,indexentry.standardindex,indexentry.excellentindex", new QFilter[]{reportProFilter, resultFilter, statusFilter});
            DynamicObject proApp = BusinessDataServiceHelper.loadSingle("pmas_pro_approval", "indexentry,indexentry.indexname,indexentry.declarevalue,indexentry.indexwarnlight,indexentry.standardindex,indexentry.excellentindex,indexentry.zmee_textfield", new QFilter[]{proFilter, statusFilter});
            DynamicObjectCollection proIndexEntries;
            Iterator var9;
            DynamicObject proIndexEntry;
            int row;
            if (report != null && report.getDynamicObjectCollection("indexentry") != null && !report.getDynamicObjectCollection("indexentry").isEmpty()) {
                proIndexEntries = report.getDynamicObjectCollection("indexentry");
                var9 = proIndexEntries.iterator();

                while(var9.hasNext()) {
                    proIndexEntry = (DynamicObject)var9.next();
                    row = this.getModel().createNewEntryRow("indexentry");
                    this.getModel().setValue("indexname", proIndexEntry.get("indexname"), row);
                    this.getModel().setValue("standardindex", proIndexEntry.get("standardindex"), row);
                    this.getModel().setValue("excellentindex", proIndexEntry.get("excellentindex"), row);
                    this.getModel().setValue("reportdeclare", proIndexEntry.get("declarevalue"), row);
                    DynamicObjectCollection proIndex = proApp.getDynamicObjectCollection("indexentry");
                    Iterator var13 = proIndex.iterator();

                    while(var13.hasNext()) {
                        DynamicObject entry = (DynamicObject)var13.next();
                        Object proIndexNamePk = entry.getDynamicObject("indexname").getPkValue();
                        Object reportIndexNamePk = entry.getDynamicObject("indexname").getPkValue();
                        if (reportIndexNamePk.equals(proIndexNamePk)) {
                            this.getModel().setValue("prodeclare", entry.get("declarevalue"), row);
                        }
                    }
                }
            } else if (proApp != null) {
                proIndexEntries = proApp.getDynamicObjectCollection("indexentry");
                if (proIndexEntries != null) {
                    var9 = proIndexEntries.iterator();

                    while(var9.hasNext()) {
                        proIndexEntry = (DynamicObject)var9.next();
                        row = this.getModel().createNewEntryRow("indexentry");
                        this.getModel().setValue("indexname", proIndexEntry.get("indexname"), row);
                        this.getModel().setValue("standardindex", proIndexEntry.get("standardindex"), row);
                        this.getModel().setValue("excellentindex", proIndexEntry.get("excellentindex"), row);
                        this.getModel().setValue("zmee_textfield", proIndexEntry.get("zmee_textfield"), row);
                        this.getModel().setValue("prodeclare", proIndexEntry.get("declarevalue"), row);
                    }
                }
            }
        }

    }
}
